package com.powernode.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.TagConstant;
import com.powernode.domain.ProdTag;
import com.powernode.domain.ProdTagReference;
import com.powernode.mapper.ProdTagMapper;
import com.powernode.mapper.ProdTagReferenceMapper;
import com.powernode.service.ProdTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.ProdTagServiceImpl")
public class ProdTagServiceImpl extends ServiceImpl<ProdTagMapper, ProdTag> implements ProdTagService{
    @Autowired
    private ProdTagMapper prodTagMapper;

    @Autowired
    private ProdTagReferenceMapper prodTagReferenceMapper;

    @Override
    public void selectProdTagPage(Page<ProdTag> page, ProdTag prodTag) {
        // 用户输入的就用模糊查询
        // 如果和like和eq同时存在的情况下，优先写eq方法，因为eq方法会创建索引
        prodTagMapper.selectPage(page, new LambdaQueryWrapper<ProdTag>()
            .eq(prodTag.getStatus() != null, ProdTag::getStatus, prodTag.getStatus())
            .like(StrUtil.isNotBlank(prodTag.getTitle()), ProdTag::getTitle, prodTag.getTitle())
            .orderByDesc(ProdTag::getSeq));
    }

    @Override
    @Cacheable(key = TagConstant.FRONT_TAG_LIST)
    public List<ProdTag> selectFrontProdTagList() {
        return prodTagMapper.selectList(new LambdaQueryWrapper<ProdTag>()
            .eq(ProdTag::getStatus, 1).orderByDesc(ProdTag::getSeq)
        );
    }

    @Override
    public Page<ProdTagReference> selectProdTagReferencePageByTagIdAndSize(Long tagId, Integer size) {
        Page<ProdTagReference> prodTagReferencePage = new Page<>(1, size);
        prodTagReferenceMapper.selectPage(prodTagReferencePage, new LambdaQueryWrapper<ProdTagReference>()
            .eq(ProdTagReference::getStatus, 1)
                .eq(ProdTagReference::getTagId, tagId)
        );
        return prodTagReferencePage;
    }

    @Override
    @Caching( evict = {
        @CacheEvict(key = TagConstant.TAG_LIST),
        @CacheEvict(key = TagConstant.FRONT_TAG_LIST)
    })
    public boolean save(ProdTag entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setShopId(1L);
        entity.setIsDefault(1);
        entity.setProdCount(0L);

        return prodTagMapper.insert(entity) > 0;
    }


    @Override
    @Caching( evict = {
            @CacheEvict(key = TagConstant.TAG_LIST),
            @CacheEvict(key = TagConstant.FRONT_TAG_LIST)
    })
    public boolean updateById(ProdTag entity) {
        entity.setUpdateTime(new Date());

        return prodTagMapper.updateById(entity) > 0;
    }


    @Override
    @Caching( evict = {
            @CacheEvict(key = TagConstant.TAG_LIST),
            @CacheEvict(key = TagConstant.FRONT_TAG_LIST)
    })
    public boolean removeById(Serializable id) {

        return prodTagMapper.deleteById(id) > 0;
    }

    @Override
    @Cacheable(key = TagConstant.TAG_LIST)
    public List<ProdTag> list() {
        List<ProdTag> tagList = prodTagMapper.selectList(new LambdaQueryWrapper<ProdTag>().eq(ProdTag::getStatus, 1).
                orderByDesc(ProdTag::getSeq));
        return tagList;
    }
}
